<?php
 
class MailAPI {
    function mail() {
    	$variable = file_get_contents('https://choku-shoku.firebaseio.com/registered.json');
		$decoded = json_decode($variable);
		$json = json_decode(file_get_contents("php://input"));
		$h = $json->header;
		$m = $json->main;
		$f = $json->footer;
		$es = $json->emailSubject;
		$fe = $json->fromEmail;
		$fn = $json->fromName;
		$rn = $json->recipientName;
		$re = $json->recipientEmail;
		//echo $email;
       				/*
				 * 
				 * header : "Click to edit header",
        main : "Click to edit main",
        footer : "Click to edit footer",

        emailSubject : '',
        fromEmail : '',
        fromName : '',
        recipientName : '',
        recipientEmail : '',
        email : "ecancil@gmail.com"
				 */
				
		$mandrill_key = 'tHhV7OsYFKcrpswvLd07Jg';
		$mandrill_base_url = 'https://mandrillapp.com/api/1.0';
		$mandrill_template_send = '/messages/send-template.json';
		$mandrill_template_name = 'Registered';
		
		$mandrill_header_content = $h;
		$mandrill_main_content = $m;
		$mandrill_footer_content = $f;
		
		$mandrill_template_content = array(
									array(
									'name' => 'header',
									'main' => $mandrill_main_content,
									'content' => $mandrill_header_content),
									array(
									'name' => 'main',
									'main' => $mandrill_main_content,
									'content' => $mandrill_main_content),
									array(
									'name' => 'footer',
									'main' => $mandrill_main_content,
									'content' => $mandrill_footer_content));
									
								
		$mandrill_email_subject = $es;
		$mandrill_from_email = $fe;
		$mandrill_from_name = $fn;
		$mandrill_recipient_name = $rn;
		$mandrill_recipient_email = $re;
		$mandrill_recipients = array(array('email'=>$mandrill_recipient_email, 'name'=>$mandrill_recipient_name, 'type'=>'to'));
		$mandrill_headers = array('Reply-To'=>'chokuShoku@chokushoku.com');
								
		$mandrill_message_content = array('subject'=>$mandrill_email_subject, 'from_email'=>$mandrill_from_email, 'from_name'=>$mandrill_from_name, 'to'=>$mandrill_recipients, 'headers'=>$mandrill_headers);											
		
		$mandrill_email_data = array(
						'key' => $mandrill_key,
					    'template_name' => $mandrill_template_name,
					    'template_content' => $mandrill_template_content,
					    'message'=>$mandrill_message_content);
		$mandrill_post_data = json_encode($mandrill_email_data);
		
		//var_dump($mandrill_post_data);
		
		//post to mandrill
		$url = $mandrill_base_url . '' . $mandrill_template_send;

		$ch = curl_init($url);
		 
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $mandrill_post_data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		 
		$response = curl_exec($ch);
		//echo $response;
		curl_close($ch);
		//header('Content-Type: application/json');
		echo json_encode(array('status' => $response));
		return;

    }
}
 

$api = new MailAPI;
$api->mail();
 
?>